package com.treasure.hunt.ad

import com.blankj.utilcode.util.LogUtils
import com.bytedance.sdk.openadsdk.*
import com.module.lemlin.base.BaseAbstractActivity
import com.treasure.hunt.R
import com.treasure.hunt.config.TTAdManagerHolder

class FullScreenVideoActivity : BaseAbstractActivity() {

    private var mTTAdNative: TTAdNative? = null

    override val layoutResId: Int
        get() = R.layout.activity_full_screen_video

    override fun initView() {
        //step1:初始化sdk
        val ttAdManager: TTAdManager = TTAdManagerHolder.get()
        //step2:(可选，强烈建议在合适的时机调用):申请部分权限，如read_phone_state,防止获取不了imei时候，下载类广告没有填充的问题。
        TTAdManagerHolder.get().requestPermissionIfNecessary(this)
        //step3:创建TTAdNative对象,用于调用广告请求接口
        mTTAdNative = ttAdManager.createAdNative(applicationContext)

        loadAd()
    }

    private fun loadAd() {

        //step4:创建广告请求参数AdSlot,具体参数含义参考文档
        val adSlot: AdSlot = AdSlot.Builder()
            .setCodeId("945907308")
            .build()

        mTTAdNative?.loadFullScreenVideoAd(adSlot, object : TTAdNative.FullScreenVideoAdListener {
            override fun onFullScreenVideoAdLoad(p0: TTFullScreenVideoAd?) {
                showAd(p0)
            }

            override fun onFullScreenVideoCached() {

            }

            override fun onError(p0: Int, p1: String?) {
                toast("${p0}$p1")
            }
        })
    }


    private fun showAd(mttFullVideoAd: TTFullScreenVideoAd?){

        mttFullVideoAd?.setFullScreenVideoAdInteractionListener(object :TTFullScreenVideoAd.FullScreenVideoAdInteractionListener{
            override fun onSkippedVideo() {
                LogUtils.e("跳过视频播放回调")
            }

            override fun onAdShow() {
                LogUtils.e("广告的展示回调")
            }

            override fun onAdVideoBarClick() {
                LogUtils.e("广告的下载bar点击回调")
            }

            override fun onVideoComplete() {
                LogUtils.e("视频播放完毕的回调")
            }

            override fun onAdClose() {
                LogUtils.e("广告关闭的回调")
            }
        })

        mttFullVideoAd?.showFullScreenVideoAd(this,TTAdConstant.RitScenes.GAME_GIFT_BONUS, null)
    }
}